//
//  lastSearchTableViewCell.m
//  SearchControllerTestDemo
//
//  Created by apple on 2017/9/7.
//  Copyright © 2017年 apple. All rights reserved.
//

#define Cell_width  ([UIScreen mainScreen].bounds.size.width-20)

#define Width_LeftSpace 45.f                //左侧间距
#define Height_ContentBtn 18                //内容btn高度
#define Width_CancelBtn 35.f                //删除按钮高度

#import "lastSearchTableViewCell.h"

@interface lastSearchTableViewCell(){
    //左侧titleBtn
    UIButton *leftTitleBtn;
    //左侧btn
    UIButton *leftBtn;
    //右侧btn
    UIButton *rightBtn;
    //右侧titleBtn
    UIButton *rightTitleBtn;
    //左侧按钮baseView
    UIView *leftBaseView;
    //右侧按钮baseView
    UIView *rightBaseView;
}

@end

@implementation lastSearchTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = 0;
        
        [self createCell];
    }
    return self;
}

//绘制cell
-(void)createCell{
    
    leftBaseView = [[UIView alloc]init];
    [self.contentView addSubview:leftBaseView];
    leftBaseView.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
    
    rightBaseView = [[UIView alloc]init];
    [self.contentView addSubview:rightBaseView];
    rightBaseView.backgroundColor = [UIColor colorWithHexString:@"#F7F7F7"];
    
    WeakSelf(weakSelf);
    [leftBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(kProportion_Width(Width_LeftSpace)));
        make.centerY.equalTo(weakSelf.contentView.mas_centerY);
        make.width.equalTo(@(kProportion_Width(480)));
        make.height.equalTo(leftBaseView.mas_width).multipliedBy(96.f/480);
    }];
    
    [rightBaseView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(leftBaseView.mas_centerY);
        make.right.equalTo(weakSelf.contentView.mas_right).offset(-kProportion_Width(Width_LeftSpace));
        make.width.equalTo(leftBaseView.mas_width);
        make.height.equalTo(leftBaseView.mas_height);
    }];

    leftTitleBtn = [[UIButton alloc]init];
    [leftBaseView addSubview:leftTitleBtn];
    [leftTitleBtn setTitleColor:[UIColor colorWithRed:0.49f green:0.49f blue:0.49f alpha:1.00f] forState:0];
    leftTitleBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
    leftTitleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;//
    [leftTitleBtn addTarget:self action:@selector(leftTitleBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    //左右按钮图片一样
    leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftBaseView addSubview:leftBtn];
    [leftBtn setImage:[UIImage imageNamed:@"CourseList_search_clearnBtnImage"] forState:0];
    [leftBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBaseView addSubview:rightBtn];
    [rightBtn setImage:[UIImage imageNamed:@"CourseList_search_clearnBtnImage"] forState:0];
    [rightBtn addTarget:self action:@selector(leftBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    
    rightTitleBtn = [[UIButton alloc]init];
    [rightBaseView addSubview:rightTitleBtn];
    [rightTitleBtn setTitleColor:[UIColor colorWithRed:0.49f green:0.49f blue:0.49f alpha:1.00f] forState:0];
    rightTitleBtn.titleLabel.font = [UIFont systemFontOfSize:15.f];
    rightTitleBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [rightTitleBtn addTarget:self action:@selector(leftTitleBtnClick:) forControlEvents:UIControlEventTouchUpInside];

    [leftTitleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(leftBaseView.mas_left).offset(kProportion_Width(25));
        make.right.equalTo(leftBtn.mas_left).offset(-10);
        make.centerY.mas_equalTo(leftBaseView.mas_centerY);
        make.height.equalTo(@18);
    }];
    
    [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(leftTitleBtn.mas_right).offset(10);
        make.right.equalTo(leftBaseView.mas_right).offset(-kProportion_Width(30));
        make.height.width.mas_offset(kProportion_Width(Width_CancelBtn));
        make.centerY.mas_equalTo(leftTitleBtn.mas_centerY);
    }];
    
    [rightTitleBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(rightBaseView.mas_left).offset(kProportion_Width(25));
        make.right.equalTo(rightBtn.mas_left).offset(-10);
        make.centerY.mas_equalTo(rightBaseView.mas_centerY);
        make.height.equalTo(leftTitleBtn.mas_height);
    }];
    
    [rightBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(rightTitleBtn.mas_right).offset(10);
        make.right.equalTo(rightBaseView.mas_right).offset(-kProportion_Width(30));
        make.height.width.mas_offset(kProportion_Width(Width_CancelBtn));
        make.centerY.mas_equalTo(rightTitleBtn.mas_centerY);
    }];
    
}

#pragma mark cell赋值方法

-(void)setContentOfCellWithModel:(NSString *)model andIndex:(NSUInteger)indexPath{
    if (indexPath % 2 == 0) {
        [leftTitleBtn setTitle:model forState:0];
        rightBaseView.hidden = YES;
        
        leftTitleBtn.tag = indexPath;
        leftBtn.tag = indexPath;
    }else{
        [rightTitleBtn setTitle:model forState:0];
        rightBaseView.hidden = NO;
        
        rightTitleBtn.tag = indexPath;
        rightBtn.tag = indexPath;
    }
    
}
#pragma mark 添加代理方法

-(void)leftTitleBtnClick:(UIButton *)btn{
    NSLog(@"----%ld",btn.tag);
    if ([self.delegate respondsToSelector:@selector(delegateOfTitleBtnClickOfWitch:)]) {
        [self.delegate delegateOfTitleBtnClickOfWitch:btn.tag];
    }
}

-(void)leftBtnClick:(UIButton *)btn{
    NSLog(@"----%ld",btn.tag);
    if ([self.delegate respondsToSelector:@selector(delegateOfDelBtnClickOfWitch:)]) {
        [self.delegate delegateOfDelBtnClickOfWitch:btn.tag];
    }
}

@end
